using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

/* =====================================================================

  File:      Register.aspx.cs for Adventure Works Cycles Storefront Sample
  Summary:   Allows the user to create a new login
  Date:	     June 16, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

namespace Microsoft.Samples.SqlServer {

    public partial class Register : System.Web.UI.Page
    {

    
//         public Register() {
//             Page.Init += new System.EventHandler(Page_Init);
//         }

        //*******************************************************
        //
        // The RegisterButton_Click event handler is used on this page to
        // add a new user into the AdventureWorks Customers database.
        //
        // The event handler then migrates any items stored in the user's
        // temporary (non-persistent) shopping cart to their
        // permanent customer account, adds a cookie to the client
        // (so that we can personalize the home page's welcome
        // message), and then redirects the browser back to the
        // originating page.
        //
        //*******************************************************

        protected void RegisterButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            // Only attempt a login if all form fields on the page are valid
            if (Page.IsValid == true) {

                // Store off old temporary shopping cart ID
                ShoppingCartDB shoppingCart = new ShoppingCartDB();
                String tempCartId = shoppingCart.GetShoppingCartId();

                // Add New Customer to CustomerDB database
                CustomersDB accountSystem = new CustomersDB();
				try 
				{
					String customerId = accountSystem.AddCustomer(
						SettingsControl.FirstNameText, SettingsControl.LastNameText, SettingsControl.EmailText,
						SettingsControl.PasswordText);

					// Set the user's authentication name to the customerId
					FormsAuthentication.SetAuthCookie(customerId, false);

					// Migrate any existing shopping cart items into the permanent shopping cart
					shoppingCart.MigrateCart(tempCartId, customerId);

					// Store the user's fullname in a cookie for personalization purposes
					String fullName = SettingsControl.FirstNameText + " " +
					  SettingsControl.LastNameText;

					//We need to Base64 this unicode string because cookies do not support unicode characters.
					UnicodeEncoding unicode = new UnicodeEncoding();

					Response.Cookies["AdventureWorks_FullName"].Value =
					  Convert.ToBase64String(unicode.GetBytes(fullName));

					SettingsControl.OnAccountSettingsUpdate();

					// Redirect browser back to shopping cart page
					Response.Redirect("Demographics.aspx");
				}
				catch (UserAlreadyExistsException) 
				{
					SettingsControl.ErrorMessage = "Registration failed:&nbsp; That email address is already registered.<br><img align=left height=1 width=92 src=images/1x1.gif>";
				}
            }
        }
        
        private void Page_Load(object sender, System.EventArgs e) {
            // Put user code to initialize the page here
        }

        private void Page_Init(object sender, EventArgs e) {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
        }

		#region Web Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {    
			this.RegisterButton.Click += new System.Web.UI.ImageClickEventHandler(this.RegisterButton_Click);

		}

		

		#endregion

    }
}
